package com.ctrlspace.util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.ctrlspace.config.ConfigurationManager;

public class Database {
	
	private static Database _instance;
	
	public static Database getInstance() {
		// Lazy initialization
		if (_instance == null) {
			_instance = new Database();
		}
		
		return _instance;
	}

	// TODO: Refactor to Factory Pattern to support other DBMS vendors (optional)
	public Connection getConnection() {
		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection((String) ConfigurationManager.get("DATABASE_URL") + "/" + ConfigurationManager.get("DATABASE_SCHEMA"),
					(String) ConfigurationManager.get("DATABASE_USERNAME"), (String) ConfigurationManager.get("DATABASE_PASSWORD"));
		} catch (ClassNotFoundException e) {
			// TODO: Refactor to custom exception
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO: Refactor to custom exception
			e.printStackTrace();
		}
		
		return conn;
	}
}
